package com.dream.keigezhushou.Activity.acty.play;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dream.keigezhushou.Activity.Uiutils.JsonParse;
import com.dream.keigezhushou.Activity.Uiutils.PrefUtils;
import com.dream.keigezhushou.Activity.Uiutils.ToastUtils;
import com.dream.keigezhushou.Activity.activity.BaseActivity;
import com.dream.keigezhushou.Activity.acty.login.RegisterResponse;
import com.dream.keigezhushou.Activity.adapter.GameEvaluateAdapter;
import com.dream.keigezhushou.Activity.adapter.GameImageAdapter;
import com.dream.keigezhushou.Activity.bean.GameBean;
import com.dream.keigezhushou.Activity.url.NetURL;
import com.dream.keigezhushou.Activity.view.CircleImageView;
import com.dream.keigezhushou.Activity.view.NestedScrollViewBottom;
import com.dream.keigezhushou.Activity.wigdt.GridSpacingItemDecoration;
import com.dream.keigezhushou.Activity.wigdt.SpacingItem;
import com.dream.keigezhushou.R;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GameInfoActivity extends BaseActivity implements View.OnClickListener{

    @BindView(R.id.iv_return)
    ImageView ivReturn;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.iv_user_pic)
    CircleImageView ivUserPic;
    @BindView(R.id.tv_user_nick)
    TextView tvUserNick;
    @BindView(R.id.tv_report)
    TextView tvReport;
    @BindView(R.id.tv_zan)
    TextView tvZan;
    @BindView(R.id.tv_date)
    TextView tvDate;
    @BindView(R.id.tv_describe)
    TextView tvDescribe;
    @BindView(R.id.rv_image)
    RecyclerView rvImage;
    @BindView(R.id.tv_all_evaluate)
    TextView tvAllEvaluate;
    @BindView(R.id.rv_evaluate)
    RecyclerView rvEvaluate;
    @BindView(R.id.scroll_bottom)
    NestedScrollViewBottom scrollBottom;
    @BindView(R.id.rl_write_evaluate)
    RelativeLayout rlWriteEvaluate;
    @BindView(R.id.ll_no_net)
    LinearLayout llNoNet;
    private GameImageAdapter imageAdapter;
    private GameEvaluateAdapter evaluateAdapter;
    private Intent intent;
    private String articleId;
    private String userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_info);
        ButterKnife.bind(this);

        articleId = this.getIntent().getStringExtra("articleId");

        ToastUtils.showToast(this,articleId);
        initView();
        initData();
    }

    //初始化界面
    public void initView() {

        tvTitle.setText("玩法详情");

        //获取到userId
        userId = PrefUtils.getString(this,"userId",null);


        loadData();




    }

    //获取网络数据
    public void loadData() {

        OkHttpUtils.post()
                .url(NetURL.GameDetails)
                .addParams("article_id", "1")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {

                        //网络请求失败时
                        scrollBottom.setVisibility(View.GONE);
                        llNoNet.setVisibility(View.VISIBLE);
                        rlWriteEvaluate.setVisibility(View.GONE);

                        //检查网络之后点击屏幕，重新进行网络请求
                        llNoNet.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {

                                loadData();
                            }
                        });

                    }

                    @Override
                    public void onResponse(String response) {

                        //网络请求成功
                        scrollBottom.setVisibility(View.VISIBLE);
                        llNoNet.setVisibility(View.GONE);
                        rlWriteEvaluate.setVisibility(View.VISIBLE);

                        GameBean gameInfo = JsonParse.getFromJson(response,GameBean.class);

                        tvUserNick.setText(gameInfo.getResults().getNickname());
                        tvDate.setText(gameInfo.getResults().getDateline());
                        tvDescribe.setText(gameInfo.getResults().getContent());
                        tvZan.setText(gameInfo.getResults().getFabnums());

                        //玩法点赞
                        tvZan.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {

                                fabulousGame();
                            }
                        });
                        tvReport.setVisibility(View.GONE);
                        tvAllEvaluate.setText("全部评论(" + gameInfo.getResults().getComments() + ")");

                        //图片列表
                        rvImage.setLayoutManager(new LinearLayoutManager(GameInfoActivity.this, LinearLayoutManager.VERTICAL, false) {
                            @Override
                            public boolean canScrollVertically() {
                                return false;
                            }
                        });

                        //列表里的图片集合

                        List<String> imageList = gameInfo.getResults().getCover();


                        int spacing = 24;
                        rvImage.addItemDecoration(new SpacingItem(spacing));
                        rvImage.setFocusable(false);
                        imageAdapter = new GameImageAdapter(GameInfoActivity.this,imageList);
                        rvImage.setAdapter(imageAdapter);

                        //评论列表
                        rvEvaluate.setLayoutManager(new LinearLayoutManager(GameInfoActivity.this, LinearLayoutManager.VERTICAL, false) {

                            @Override
                            public boolean canScrollVertically() {
                                return false;
                            }
                        });

                        List<GameBean.CommentsBean> commentsList = gameInfo.getComments();
                        rvEvaluate.setFocusable(false);
                        evaluateAdapter = new GameEvaluateAdapter(GameInfoActivity.this,commentsList);
                        rvEvaluate.setAdapter(evaluateAdapter);
                    }
                });

    }


    //玩法点赞
    public  void fabulousGame(){

        OkHttpUtils.post()
                .url(NetURL.FabulousGame)
                .addParams("userId",userId)
                .addParams("article_id",articleId)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {

                        RegisterResponse message = JsonParse.getFromJson(response,RegisterResponse.class);

                        String strMessage = message.getMessage();
                        String state = message.getStatus();

                        int status = Integer.parseInt(state);


                    }
                });

    }

    //初始化数据
    public void initData() {

        //滑动到底部的监听
        scrollBottom.setScrollBottomListener(new NestedScrollViewBottom.ScrollBottomListener() {
            @Override
            public void scrollBottom() {

            }
        });

        ivReturn.setOnClickListener(this);
        rlWriteEvaluate.setOnClickListener(this);
    }


    //点击事件的实现

    @Override
    public void onClick(View view) {


        switch (view.getId()){

            case R.id.iv_return:

                //返回
                finish();
                break;

            case R.id.rl_write_evaluate:

                intent = new Intent(this,GameEvaluateActivity.class);
                intent.putExtra("articleId",articleId);
                startActivity(intent);
                break;
        }
    }
}
